import matplotlib.pylab as plt 
import matplotlib
matplotlib.style.use('ggplot')
import pandas as pd
from pandas import Series
import os
from config import BASE_LOCAL_PATH
from functools import reduce




class Statistc(object):
    """
    you can add a new way to statistic , just implement a new method like this

    def xxx(self,city_name)
    """
    def __init__(self,city,path=BASE_LOCAL_PATH):
        self.citys =  city.split()
        self.data = pd.Series.from_csv(os.path.join(path,city))
        # plt.ion()
        plt.show()

    @staticmethod
    def read_all_city(path=BASE_LOCAL_PATH):
        files = os.listdir(path)
        dat = pd.DataFrame()
        sers = [ pd.DataFrame(pd.Series.from_csv(os.path.join(path,city)),columns=[city]) for city in files]
        return reduce(lambda x,y: x.join(y).fillna(50), sers)
            

    # def line(self,data):
    #     data.plot()
        

    # def bar(self,data):
    #     data.pltot(kind="bar")

    # def hist(self,data):
    #     data.pltot(kind="hist")

    # def hist(self):
    #     data.pltot(kind="hist")
    
    # def kde(self):
    #     self.data.pltot(kind='kde')

    # def pie(self):
    #     self.data.pltot(kind="pie")


def get_all_method(class_obj):
    return [ i for i in dir(class_obj) if not i.startswith("_")]

class DealData(Statistc):
    
    def every_dateformat_mean(self,way,date="month"):

        """
        this method 
        """
        if date == "month":
            self.data.index = self.data.index.month
        elif date == "week":
            self.data.index = self.data.index.week
        elif date == "year":
            self.data.index = self.data.index.year

        self.data =  self.data.groupby(level=0).mean()
        self.data.plot(kind=way,title=self.citys)

    def classfy_air_rank(self):
        c1 = self.data.loc[(self.data  <= 50 )]
        c2 = self.data.loc[(self.data  >50 ) & (self.data  <= 100 ) ]
        c3 = self.data.loc[(self.data  >100 ) & (self.data  <= 150 ) ]
        c4 = self.data.loc[(self.data  >150) & (self.data  <= 200 ) ]
        c5 = self.data.loc[(self.data  >200 ) & (self.data  <= 300 ) ]
        c6 = self.data.loc[(self.data  >300) ]
        new_data = pd.Series([
                c6.count(),
                c2.count(),
                c3.count(),
                c4.count(),
                c5.count(),
                
                c1.count(),
            ],index=[
                '还是自杀比较爽',
                '良',
                '轻度污染',
                '中度污染',
                '重度污染',
                
                '优',
            ])
        new_data.plot(kind='pie',figsize=(10,10),autopct='%.2f',fontsize=10,title=self.citys)

